package com.yanqu.road.dao.impl.activity.seacraft;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaBattleConfig;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaFloorConfig;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaGuessConfig;
import com.yanqu.road.entity.activity.seacraft.config.SeacraftSouthSeaShipConfig;
import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 11:28
 */
public class SeacraftSouthSeaConfigDaoImpl extends TempDao {


    public static String getSeacraftSouthSeaShipConfigSql = "select * from `t_s_activity_seacraft_ship` where `activityId`=?";

    public List<SeacraftSouthSeaShipConfig> getSeacraftSouthSeaShipConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SeacraftSouthSeaShipConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaShipConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaShipConfig shipConfig = parseSeacraftSouthSeaShipConfig(rs);
                list.add(shipConfig);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String getSeacraftSouthSeaGuessConfigSql = "select * from `t_s_activity_seacraft_guess` where `activityId`=?";

    public List<SeacraftSouthSeaGuessConfig> getSeacraftSouthSeaGuessConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SeacraftSouthSeaGuessConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaGuessConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseSeacraftSouthSeaGuessConfig(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    private static String getSeacraftSouthSeaFloorConfigSql = "select * from `t_s_activity_seacraft_floor` where `activityId`=?";

    public List<SeacraftSouthSeaFloorConfig> getSeacraftSouthSeaFloorConfigList(int activityId) {
        List<SeacraftSouthSeaFloorConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaFloorConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                SeacraftSouthSeaFloorConfig seacraftSouthSeaFloorConfig = new SeacraftSouthSeaFloorConfig();
                seacraftSouthSeaFloorConfig.setActivityId(rs.getInt("activityId"));
                seacraftSouthSeaFloorConfig.setFloor(rs.getInt("floor"));
                seacraftSouthSeaFloorConfig.setSpecialReward(rs.getString("specialReward"));
                seacraftSouthSeaFloorConfig.setRewards(rs.getString("rewards"));
                seacraftSouthSeaFloorConfig.setConsume(rs.getString("consume"));

                String[] rewardList = seacraftSouthSeaFloorConfig.getRewards().split("\\|");
                for (String str : rewardList) {
                    String[] item = str.split(",");
                    seacraftSouthSeaFloorConfig.getWeightList().add(new PropertyWeightItem(PropertyHelper.parseStringToProperty(item[0]), Integer.valueOf(item[1])));
                }
                String[] specialRewardList = seacraftSouthSeaFloorConfig.getSpecialReward().split("\\|");
                if (specialRewardList.length > 0) {
                    seacraftSouthSeaFloorConfig.setSpecialRewardStr(specialRewardList[0]);
                    if (specialRewardList.length > 1) {
                        List<String> rewardList2 = StringUtils.stringToStringList(specialRewardList[1], ",");
                        for (String string : rewardList2) {
                            String[] item = string.split(";");
                            seacraftSouthSeaFloorConfig.getSpecialRewardList().add(new PropertyWeightItem(PropertyHelper.parseStringToProperty(item[0]), Integer.valueOf(item[1])));
                        }
                    }
                }
                list.add(seacraftSouthSeaFloorConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getSeacraftSouthSeaBattleConfigSql = "select * from `t_s_activity_seacraft_battle` where `activityId`=?";

    public List<SeacraftSouthSeaBattleConfig> getSeacraftSouthSeaBattleConfigList(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<SeacraftSouthSeaBattleConfig> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getSeacraftSouthSeaBattleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parseSeacraftSouthSeaBattleConfig(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }


    protected SeacraftSouthSeaShipConfig parseSeacraftSouthSeaShipConfig(ResultSet rs) throws SQLException {
        SeacraftSouthSeaShipConfig seacraftSouthSeaShipConfig = new SeacraftSouthSeaShipConfig();
        seacraftSouthSeaShipConfig.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaShipConfig.setLevel(rs.getInt("level"));
        seacraftSouthSeaShipConfig.setExp(rs.getInt("exp"));
        seacraftSouthSeaShipConfig.setSpeed(rs.getInt("speed"));
        seacraftSouthSeaShipConfig.setHealth(rs.getInt("health"));
        seacraftSouthSeaShipConfig.setBonus(rs.getInt("bonus"));
        seacraftSouthSeaShipConfig.setShip(rs.getInt("ship"));

        return seacraftSouthSeaShipConfig;
    }

    protected SeacraftSouthSeaGuessConfig parseSeacraftSouthSeaGuessConfig(ResultSet rs) throws SQLException {
        SeacraftSouthSeaGuessConfig seacraftSouthSeaGuessConfig = new SeacraftSouthSeaGuessConfig();
        seacraftSouthSeaGuessConfig.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaGuessConfig.setId(rs.getInt("id"));
        seacraftSouthSeaGuessConfig.setRound(rs.getInt("round"));
        seacraftSouthSeaGuessConfig.setCount(rs.getInt("count"));
        seacraftSouthSeaGuessConfig.setDesc(rs.getString("desc"));
        seacraftSouthSeaGuessConfig.setAward(rs.getString("award"));
        seacraftSouthSeaGuessConfig.setShow(rs.getInt("show"));

        return seacraftSouthSeaGuessConfig;
    }


    protected SeacraftSouthSeaBattleConfig parseSeacraftSouthSeaBattleConfig(ResultSet rs) throws SQLException {
        SeacraftSouthSeaBattleConfig seacraftSouthSeaShipConfig = new SeacraftSouthSeaBattleConfig();
        seacraftSouthSeaShipConfig.setActivityId(rs.getInt("activityId"));
        seacraftSouthSeaShipConfig.setId(rs.getInt("id"));
        seacraftSouthSeaShipConfig.setRound(rs.getInt("round"));
        seacraftSouthSeaShipConfig.setType(rs.getInt("type"));
        seacraftSouthSeaShipConfig.setVictoryTime(rs.getInt("victoryTime"));
        seacraftSouthSeaShipConfig.setRank(rs.getInt("rank"));
        seacraftSouthSeaShipConfig.setAward(rs.getInt("award"));

        return seacraftSouthSeaShipConfig;
    }

}
